package sg.edu.nus.ivletalk.test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	Button button;
	SpeechRecognizer recognizer;
	boolean listening = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText editText = (EditText) findViewById(R.id.editText1);
        RecognitionListener listener = new RecognitionListenerImpl(editText);
        button = (Button) findViewById(R.id.button1);
        recognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
        recognizer.setRecognitionListener(listener);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    public void toggle(View view){
    	if(listening){
    		button.setText("Listen");
    		recognizer.stopListening();
    		listening=false;
    	} else {
    		button.setText("Stop");
    		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);        
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"sg.edu.nus.ivletalk.test");

            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); 
                 recognizer.startListening(intent);    		listening=true;
    	}
    }
    
}
